<?php
namespace Kuoqi\Html;

class AHtml{

    public $text;
    public $href;
    public $tips = '提示！';
    public $innerStr = '';
    public $classname;
    public $data = array();

    //数据加载方式
    //默认原生加载，使用a 的href
    //使用跳转页面加载的 target
    //使用代码片段加载的 page
    public $datatype = '';

    public function __construct($opts){
        $this->text = isset($opts['text'])?$opts['text']:'';
        $this->tips = isset($opts['tips'])?$opts['tips']:'';
        $this->href = isset($opts['href'])?$opts['href']:'';
        $this->datatype = isset($opts['datatype'])?$opts['datatype']:'';
        $this->classname = isset($opts['classname'])?$opts['classname']:'';
        $this->data = isset($opts['data'])?$opts['data']:'';
    }

    public function flushToString(){
        $this->innerStr = sprintf('<a %s %s %s %s>%s</a>',
            $this->classname?('class=\''.$this->classname.'\''):'',
            $this->doDatatype(),
            $this->tips?('data-tips=\''.$this->tips.'\''):'',
            $this->doData(),
            $this->text
        );
        return $this->innerStr;
    }

    //关于datatype的数据处理
    private function doDatatype(){
        switch(strtolower($this->datatype)){
            case 'page':
                return $this->href?('data-page=\''.$this->href.'\''):'';
                break;
            case 'ajax';
                return $this->href?('data-ajax=\''.$this->href.'\''):'';
                break;
            case 'ajax-del';
                return $this->href?('data-ajax-del=\''.$this->href.'\''):'';
                break;
            default:return $this->href?('href=\''.$this->href.'\''):'';
        }
    }

    //关于data
    private function doData(){
        $html = '';
        if(is_array($this->data)){
            foreach($this->data as $k=>$item){
                $html .= sprintf('data-%s="%s"',$k,$item);
            }
        }
        return $html;
    }

}